module('Assistant');

test('Assist', function() {
    var bronxvilleAP = {
        name: 'A&P Bronxville',
        type: PlaceType.superMarket,
        address: '12 Cedar Street, Bronxville',
        lat: 40.938817,
        lng: -73.833684
    };
    var whitePlainsBrazenFox = {
        name: 'Brazen Fox',
        type: PlaceType.bar,
        address: '175 Mamaroneck Avenue, White Plains',
        lat: 41.028486,
        lng: -73.764737
    };
    var whitePlainsGalleria = {
        name: 'The Galleria',
        type: PlaceType.mall,
        address: '100 Main Street, White Plains',
        lat: 41.031817,
        lng: -73.770817
    };

    var environment = new MockEnvironment({
        events: [
            { time: '16:40', location: { address: '64 Sagamore Road, Bronxville', lat: 40.945695, lng: -73.831908 } },
            { time: '17:00', location: { address: '77 Pondfield Road, Bronxville', lat: 40.94032, lng: -73.833328 } },
            { time: '19:00', location: { address: '1 N Lexington Avenue, White Plains', lat: 41.031982, lng: -73.77249 } }
        ],

        placeService: new MockPlaceService({
            places: [ bronxvilleAP, whitePlainsGalleria, whitePlainsBrazenFox ]
        })
    });

    var tasks = [
        Action.buy({ item: 'tomatoes' }),
        Action.buy({ item: 'dishware' }),
        Action.meet({ entity: 'John', location: whitePlainsGalleria })
    ];

    var expectedSuggestions = [
        Action.buy({ time: '17:00', item: 'tomatoes', location: bronxvilleAP }),
        Action.buy({ time: '19:00', item: 'dishware', location: whitePlainsGalleria }),
        Action.meet({ time: '19:00', entity: 'John', location: whitePlainsGalleria })
    ];

    var assistant = new Assistant({
        environment: environment,
        tasks: tasks
    });

    var suggestions = [];
    assistant.suggest(function(task) {
        console.log('Suggestion: at ' + task.time + ',', task.action, task.item || task.entity, 'at', task.location.address);
        start();
        suggestions.push(task);
        if (suggestions.length == expectedSuggestions.length) {
            var comparator = function(a1, a2) {
                var r = a1.time.localeCompare(a2.time);
                return r || a1.action.localeCompare(a2.action);
            };
            suggestions.sort(comparator);
            expectedSuggestions.sort(comparator);
            deepEqual(suggestions, expectedSuggestions);
        }
    });

    expect(1);
    stop(expectedSuggestions.length);

    environment.start();
});
